import 'package:color_dart/color_dart.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/components/labels/index.dart';
import 'package:im_flutter_sdk_example/jsonserialize/group/data.dart';
import 'package:im_flutter_sdk_example/jsonserialize/group/group_manager.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/home/widgets/top_search.dart';
import 'package:im_flutter_sdk_example/provider/group_formdate_model.dart';
import 'package:im_flutter_sdk_example/utils/constants.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:provider/provider.dart';

class TeamMangerItem extends StatelessWidget {
  final GroupManager item;
  final Function onChecked;
  const TeamMangerItem({Key key, this.item, this.onChecked}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 10),
      child: ListTile(
          title: Container(
            child: Row(
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 6),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                          width: 150,
                          child: Text(
                            item.userInfo.nickname,
                            style: mainTitleStyle,
                            overflow: TextOverflow.ellipsis,
                          )),
                      item.role == 1
                          ? Text(
                              "管理员",
                              style: TextStyle(
                                  color: rgba(117, 117, 117, 1), fontSize: 12),
                            )
                          : Container()
                    ],
                  ),
                ),
                // Container(
                //     margin: EdgeInsets.only(left: 10),
                //     child: Row(children: [LabelExpert()]))
              ],
            ),
          ),
          leading: SizedBox(
            width: 60,
            height: 60,
            child: ClipOval(
              child: Image.network(
                '${item.userInfo.avatar}',
                width: 30,
                height: 30,
                fit: BoxFit.cover,
              ),
            ),
          ),
          trailing: Column(
            children: [
              Checkbox(
                value: item.checked == null ? false : item.checked,
                onChanged: (value) {
                  onChecked(value, item.userId);
                },
              )
            ],
          )),
    );
  }
}

class TeamManger extends StatefulWidget {
  const TeamManger({Key key}) : super(key: key);

  @override
  _TeamMangerState createState() => _TeamMangerState();
}

class _TeamMangerState extends State<TeamManger> {
  GroupFormDataModel _formData;

  @override
  Widget build(BuildContext context) {
    if (_formData == null) return G.emptyBox();
    if (_formData.groupManager == null) return G.emptyBox();
    return Scaffold(
        appBar: AppBar(title: Text('管理小组')),
        body: SafeArea(
          child: Stack(
            children: [
              SingleChildScrollView(
                child: Container(
                  height: G.screenHeight(),
                  color: rgba(255, 255, 255, 1),
                  child: Column(
                    children: [
                      Container(
                        child: TopSearchbar(),
                      ),
                      Column(
                        children: _formData.groupManager.data
                            .map((item) => TeamMangerItem(
                                  item: item,
                                  onChecked: (v, id) => {onChecked(v, id)},
                                ))
                            .toList(),
                      ),
                    ],
                  ),
                ),
              ),
              Positioned(
                bottom: 10,
                width: G.screenWidth(),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    _formData.myGroup.owner
                        ? (GestureDetector(
                            onTap: _setAdmin,
                            child: Container(
                                height: 45,
                                width: G.screenWidth() * 0.9,
                                color: rgba(42, 125, 250, 1),
                                child: Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Text(
                                        '确定',
                                        style: TextStyle(
                                            color: rgba(255, 255, 255, 1),
                                            fontSize: 16),
                                      )
                                    ])),
                          ))
                        : G.emptyBox(),
                  ],
                ),
              )
            ],
          ),
        ));
  }

  List<GroupManager> listData = [];
  List<Group> myGroup = [];
  @override
  void initState() {
    super.initState();
    _loadManagers();
  }

  _loadManagers() {
    Future.delayed(Duration.zero, () {
      _formData = Provider.of<GroupFormDataModel>(context);
      _formData.getManagers();
    });
  }

  void onChecked(v, id) {
    listData.forEach((item) {
      if (item.userId == id) {
        item.checked = v;
        print(item.checked);
      }
    });
    this.setState(() {});
  }

  _setAdmin() async {
    List<String> list = [];
    listData.forEach((item) {
      if (item.checked) {
        list.add(item.userId);
      }
    });
    Group group = myGroup[0];
    print('groupId:' + "${group.id}");
    print(list);
    return;
    var res =
        await G.req.group.setAdmin(groupId: "${group.id}", applierIds: list);
  }
}
